package com.foreknow.penTest;

/**
 * @author shkstart
 * @create 2021-08-18 20:05
 */

import java.util.*;

/**
 * 3、求其每一个子串出现的次数
 * 要求:对每个字符串，输出它所有出现的次数在1次以上的子字符串,和这个子字符串出现的次数并按字典顺序排序(由小到大)·
 * 例如:abcabcdef.
 * <p>
 * abcab
 */
public class zjtdTest1 {
    public static void main(String[] args) {
        String str = "aabcabcdef";
        String substr;
        int num = 0;//记录次数
        List<String> list = new ArrayList<>();
        List<String> towlist = new ArrayList<>();
        Map<String, Integer> map = new TreeMap<>();
        //将字符串所有的组合存到list集合中
        for (int i = 0; i < str.length(); i++) {
            for (int j = i + 1; j < str.length() + 1; j++) {
                //截取原字符串所有的组合
                substr = str.substring(i, j);
                list.add(substr);
            }

        }

        Set<String> set = new HashSet<>();
        for (int i = 0; i < list.size(); i++) {
            set.add(list.get(i));
        }

//        for (String str1:list) {
//            System.out.print(str1+" ");
//        }
        System.out.println();
        Iterator<String> iterator = set.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            for (int i = 0; i < list.size(); i++) {
                if (next.equals(list.get(i))) {
                    num++;
                }
            }
            if (num >= 2) {
//                System.out.println(next + "------" + num);
                map.put(next, num);
                towlist.add(next);
                num = 0;
            } else {
                num = 0;
            }
        }

//        for (int i = 0; i < towlist.size() - 1; i++) {
//            for (int j = 0; j < towlist.size() - 1 - i; j++) {
//                String s1 = towlist.get(j);
//                String s2 = towlist.get(j + 1);
//                char[] chars1 = s1.toCharArray();
//                char[] chars2 = s2.toCharArray();
//                for (int k = 0; k < chars1.length && k < chars2.length; k++) {
//
//                    if (chars1[k] == chars2[k] && chars1.length > chars2.length) {
//                        //交换这个list集合中的两个元素
//                        String temp = towlist.get(j);
//                        towlist.set(j, towlist.get(j + 1));
//                        towlist.set(j + 1, temp);
//                    } else if (chars1[k] > chars2[k]) {
//                        String temp = towlist.get(j);
//                        towlist.set(j, towlist.get(j + 1));
//                        towlist.set(j + 1, temp);
//                        //交换后跳出循环
//                        break;
//                    }
//                }
//            }
//
//        }

       System.out.println("***************");
//        for (String str2 : towlist) {
//            System.out.print(str2 + " ");
//        }
//        System.out.println();
//        System.out.println("------------");
//        for (int i = 0; i < towlist.size(); i++) {
//
//            Integer integer = map.get(towlist.get(i));
//            System.out.println(towlist.get(i) + "----" + integer + " ");
//
//        }
//        for (Map.Entry<String, Integer> entry:map.entrySet()) {
//            System.out.println(entry.getKey() + " " + entry.getValue());
//
//        }
//        for(Map.Entry<String,Integer> entry:map.entrySet()){
//            System.out.println(entry.getKey()+"-----"+entry.getValue());
//        }
        Set<Map.Entry<String, Integer>> entries = map.entrySet();
        Iterator<Map.Entry<String, Integer>> iterator1 = entries.iterator();
        while (iterator1.hasNext()){
            System.out.println(iterator1.next());
        }
    }

}
